package com.zlm.user.partner.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.constants.FileConstants;
import com.zlm.common.dto.HxUserDto;
import com.zlm.common.dto.userPartner.SysPartnerDto;
import com.zlm.common.entity.UserRelationDO;
import com.zlm.huanxin.util.HuanxinUtils;
import com.zlm.user.partner.dao.SysPartnerDao;
import com.zlm.user.partner.dao.UserRelationDao;
import com.zlm.user.partner.service.UserRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/7/8 13:25
 */
@Service
public class UserRelationServiceImpl extends AbstractBaseService<UserRelationDao, UserRelationDO> implements UserRelationService {

    @Autowired
    @Override
    public void setBaseDao(UserRelationDao userRelationDao) {
        this.baseDao = userRelationDao;
    }

    @Autowired
    private SysPartnerDao sysPartnerDao;

    @Override
    public void addFriend(Integer userId, String friendHxId) {
        SysPartnerDto sysPartnerDto = sysPartnerDao.queryPartnerById(userId);
        if (null == sysPartnerDto || StringUtils.isBlank(sysPartnerDto.getHxId())) {
            throw new RuntimeException(CodeConstants.CODE_103);
        }
        String userHxId = sysPartnerDto.getHxId();
        Integer count = baseDao.countUserRelation(userHxId,friendHxId);
        if (count > 0) {
            //已存在好友关系，不做任何操作
        }else {
            //新增好友关系
            baseDao.insertUserRelation(userHxId,friendHxId);
//            HuanxinUtils.addFriend(userHxId, friendHxId);
        }

    }

    @Override
    public void deleteFriend(Integer userId, String friendHxId) {
        SysPartnerDto sysPartnerDto = sysPartnerDao.queryPartnerById(userId);
        if (null == sysPartnerDto || StringUtils.isBlank(sysPartnerDto.getHxId())) {
            throw new RuntimeException(CodeConstants.CODE_103);
        }
        String userHxId = sysPartnerDto.getHxId();
        baseDao.deleteUserRelation(userHxId,friendHxId);
        HuanxinUtils.deleteFriend(userHxId,friendHxId);
    }

    @Override
    public List<HxUserDto> queryFriendList(Integer userId) {
        SysPartnerDto sysPartnerDto = sysPartnerDao.queryPartnerById(userId);
        if (null == sysPartnerDto || StringUtils.isBlank(sysPartnerDto.getHxId())) {
            throw new RuntimeException(CodeConstants.CODE_103);
        }
        String userHxId = sysPartnerDto.getHxId();
        List<HxUserDto> userDtoList = baseDao.queryFriendList(userHxId);
        for (HxUserDto userDto : userDtoList) {
            userDto.setAvatar(userDto.getAvatar()==null? "": CommonConstants.IMG_URL.concat(userDto.getAvatar()));
        }
        
        return baseDao.queryFriendList(userHxId);
    }
}
